import Vue from 'vue'
import store from '../store/index'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/', // 首页面，静态加载（速度快），在访问到根目录时打开
        name: 'home',
        meta: {
            show: false,
            title: '首页',
            icon: ''
        },
        component: HomeView,
        children: [{
                path: '/user',
                name: 'user',
                meta: {
                    show: true,
                    title: '用户管理',
                    icon: 'el-icon-user'
                },
                //懒加载，路由到此页面时才加载
                component: () => import( /* webpackChunkName: "user" */ '../views/UserView.vue')
            },
            {
                path: '/inoSite',
                name: 'inoSite',
                meta: {
                    show: true,
                    title: '接种点管理',
                    icon: ''
                },
                component: () => import( /* webpackChunkName: "inoSite" */ '../views/InoSiteView.vue')
            },
            {
                path: '/vaccine',
                name: 'vaccine',
                meta: {
                    show: true,
                    title: '疫苗管理',
                    icon: ''
                },
                component: () => import( /* webpackChunkName: "vaccine" */ '../views/VaccineView.vue')
            },
            {
                path: '/vacType',
                name: 'vacType',
                meta: {
                    show: true,
                    title: '疫苗类型管理',
                    icon: ''
                },
                component: () => import( /* webpackChunkName: "hospital" */ '../views/VacTypeView.vue')
            },
            {
                path: '/order',
                name: 'order',
                meta: {
                    show: true,
                    title: '预约接种管理',
                    icon: ''
                },
                component: () => import( /* webpackChunkName: "order" */ '../views/OrderView.vue')
            },
            {
                path: '/personal',
                name: 'personal',
                meta: {
                    show: true,
                    title: '我的预约',
                    icon: ''
                },
                component: () => import( /* webpackChunkName: "order" */ '../views/PersonalView.vue')
            },
            {
                path: '/about',
                name: 'about',
                meta: {
                    show: true,
                    title: '关于',
                    icon: ''
                },
                // route level code-splitting
                // this generates a separate chunk (about.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () => import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            show: false,
            title: '',
            icon: ''
        },
        // 懒加载（lazy-loaded）， 路由到此页面时才加载（when the route is visited.）
        component: () => import( /* webpackChunkName: "login" */ '../views/LoginView.vue')
    },
    {
        path: '/registry',
        name: 'registry',
        meta: {
            show: false,
            title: '',
            icon: ''
        },
        // 懒加载（lazy-loaded）， 路由到此页面时才加载（when the route is visited.）
        component: () => import( /* webpackChunkName: "registry" */ '../views/RegistryView.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

/**
 * 路由拦截器
 * 功能：全局导航守卫，对路由进行处理
 * 参数：
 * to 将要访问的路径
 * from 代表从那个路径跳转而来
 * next 是一个函数 表示放行
 * ---- next()：放行
 * ---- next('/home')：强制跳转到home这个页面
 */
router.beforeEach(async (to, from, next) => {
    if (to.path === '/login') {
        next(); // 正常放行
    } else {
        // 取token
        let token = localStorage.getItem('Authorization');
        console.log("导航守卫1", token);
        if (token === null || token === '') { // 如果token不存在
            console.log("导航守卫2 token==null to /login", token)
            next('/login'); // 跳转至登录页面
        } else {
            // 调用store，来分发请求，获取登录用户的信息
            await store.dispatch('getUserInfo').then((res) => {
                console.log("导航守卫，start4 用户信息：", res);
                localStorage.setItem("SET_USER", JSON.stringify(res.data.data));
                next(); //放行
            }).catch(res => {
                console.log("导航守卫，start5: ", res);
                next(); // 放行
            });
        }
    }
});

export default router
